<?php

namespace app\apicompany\controller;

use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 企业首页统计
 */
class Statistics extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\apicompany\model\ChatFriend();
        $this->modelChat = new \app\apicompany\model\ChatFriend();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }

    /**
     * 数据
     * @return void
     */
    public function info(){

        try {
            $id = $this->auth->getUser()['id'];
            //今日沟通， 投递数量， 面试数量
           $data = $this->model->where('sender_id' , $id)
                ->where("from_unixtime(createtime,'%Y-%m-%d')",date('Y-m-d'))
                ->field("ifnull(sum(case status=3 when 1 then 0 end),0) as interview_num,count(id) as communication_num,count(id) as receive_num")
               ->find();
            $this->success('ok',$data);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 列表统计
     * @return void
     */
    public function list(){

        try {
            $id = $this->auth->getUser()['id'];
            //今日沟通， 投递数量， 面试数量
            $data = $this->model->where('sender_id' , $id)
                ->where('status' , 3)
                ->group("from_unixtime(createtime,'%Y-%m-%d')")
                ->limit('7')
                ->field("from_unixtime(createtime,'%Y-%m-%d') as createtime,count(id) as num")
                ->select();
            if ($data->isEmpty()){
                $this->success('ok',[['createtime' => date('Y-m-d'),'num' => 0]]);
            }
            $this->success('ok',$data);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

}